<template>
  <el-dialog width="700px" title="预约库存" destroy-on-close append-to-body :visible.sync="show" :before-close="cancel">
    <section class="wenan-info">
      <div class="wenan-info-left">
        <div style="font-size: 16px">{{ formData.skuNo }}</div>
        <div>
          <el-image
            @click="$viewImage(formData.imgUrl)"
            style="width: 150px; height: 150px; cursor: pointer"
            fit="contain"
            :src="formData.imgUrl"
          />
        </div>
      </div>
      <div class="wenan-info-right">
        <div>
          <span>价格</span>
          <el-input disabled style="width: 200px; margin: 10px; margin-right: 5px" v-model="formData.cloudPrice">
            <i slot="suffix">JPY</i>
          </el-input>
          <span>/套</span>
        </div>
        <div>
          <span>申请数量</span>
          <el-input-number
            style="width: 200px; margin: 10px"
            v-model="form.applyNum"
            controls-position="right"
            :min="1"
          />
          <span>套</span>
        </div>
        <div>
          <span>总价格</span>
          <el-input disabled style="width: 200px; margin: 10px; margin-right: 23px" :value="totalPrice">
            <i slot="suffix">JPY</i>
          </el-input>
        </div>
        <div>
          <span>所需积分</span>
          <el-input readonly style="width: 200px; margin: 10px; margin-right: 23px" :value="totalPrice" />
        </div>
        <div>
          <span>当前积分</span>
          <el-input readonly style="width: 200px; margin: 10px; margin-right: 23px" :value="totalPoint" />
        </div>
      </div>
    </section>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleSubmit" type="primary" v-hasPermi="['distribution:skuTempProxyApply:add']">
        {{ $t('确定') }}
      </el-button>
      <el-button @click="cancel">{{ $t('关闭') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getBalanceByDistr, reservationSku } from '@/api/distribution/sku/cloudProduct'
export default {
  name: 'reservationSku',
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: () => ({}),
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      /**总积分 */
      totalPoint: 0,
      form: {
        applyNum: '',
      },
      // 查询参数
      queryParams: {},
    }
  },
  computed: {
    /**
     * @returns {string}
     */
    totalPrice() {
      return +this.form.applyNum * +this.formData.cloudPrice
    },
  },

  watch: {
    show: function (newValue) {
      this.form.num = 1
      if (newValue) {
        this.getList()
      }
    },
  },
  methods: {
    handleSubmit() {
      const data = {
        skuId: this.formData.skuId,
        skuNo: this.formData.skuNo,
        applyNum: this.form.applyNum,
        totalPrice: this.totalPrice,
      }
      reservationSku(data).then((res) => {
        const { code, data, msg } = res
        if (code === 200) {
          this.$message({ type: 'success', message: msg })
          this.$emit('getList')
          this.cancel()
        }
      })
    },
    getList() {
      getBalanceByDistr().then((res) => {
        this.totalPoint = res.data
      })
    },

    cancel() {
      this.$emit('update:show', false)
    },
  },
}
</script>
<style scoped lang="scss">
.wenan-info {
  display: flex;
  margin-top: 20px;
  &-left {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  &-right {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    margin-right: 30px;
    ::v-deep .el-input__suffix {
      display: flex;
      align-items: center;
    }
  }
}
</style>
